<!DOCTYPE html>
<html lang="zh_CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <title>添加商品</title>
    <th:block th:include="~{common/common::commonHead}"/>
</head>
<style>
    #goods{
        float:right;
        position: relative;
    }
</style>
<body data-width="850" data-height="580">
<form id="defaultForm" class="form-horizontal" role="form" th:action="@{/goods/addGoods}">
    <!--<input type="hidden" id="goodsId" name="goodsId">-->
    <div class="col-sm-6 col-xs-12">

        <div id="localImg" style="padding: 10px;border: 1px dashed #ddd;">
            <img id="preview" th:src="@{/img/type/defaultGoodsImg.png}" width="280" height="280" style="display: block; margin: auto">
        </div>
        <div class="iconic-input right">
            <button type="button" class="btn btn-blue" style="pointer-events: none; margin-left: 160px; margin-top: 10px;">
                点击上传
            </button>
            <input type="file" name="file" id="doc" style="width:78px;height: 32px; opacity: 0; position: relative; margin-top: -30px; margin-left: 160px;" onchange="setImagePreview();">
        </div>
    </div>
    <div class="col-sm-6 col-xs-12" id="goods">
        <input type="hidden" id="goodsImg" name="goodsImg">
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-md-2 col-sm-3 col-xs-4 control-label"> 商品名称 </label>
                <div class="col-md-6 col-sm-8 col-xs-8">
                    <div class="iconic-input right">
                        <input type="text" class="form-control spinner" name="goodsName" required maxlength="20" placeholder="请输入商品名称"/>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-md-2 col-sm-3 col-xs-4 control-label"> 基准价 </label>
                <div class="col-md-6 col-sm-8 col-xs-8">
                    <div class="iconic-input right">
                        <input id="goodsMarketPrice" name="goodsMarketPrice" type="number" class="form-control spinner" required data-valid-decimal="true" data-decimal="true" min="0.01" max="10000" placeholder="市场零售价格">
                    </div>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-md-2 col-sm-3 col-xs-4 control-label"> 供应价 </label>
                <div class="col-md-6 col-sm-8 col-xs-8">
                    <div class="iconic-input right">
                        <input id="goodsSellPrice" name="goodsSellPrice" type="number" class="form-control spinner" required data-valid-decimal="true" data-decimal min="0.01" max="10000" placeholder="销售价格">
                    </div>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-md-2 col-sm-3 col-xs-4 control-label"> 结算价 </label>
                <div class="col-md-6 col-sm-8 col-xs-8">
                    <div class="iconic-input right">
                        <input id="goodsSettlementPrice" name="goodsSettlementPrice" type="number" class="form-control spinner" required data-valid-decimal="true" data-decimal min="0.01" max="10000" placeholder="结算价格">
                    </div>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-md-2 col-sm-3 col-xs-4 control-label"> 商品单位 </label>
                <div class="col-md-6 col-sm-8 col-xs-8">
                    <div class="iconic-input right">
                        <input id="goodsUnit" name="goodsUnit" type="text" class="form-control spinner" required maxlength="7" placeholder="1~7位字符">
                    </div>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-md-2 col-sm-3 col-xs-4 control-label"> 规格 </label>
                <div class="col-md-6 col-sm-8 col-xs-8">
                    <div class="iconic-input right">
                        <input id="goodsSpec" name="goodsSpec" type="text" class="form-control spinner"  maxlength="60" placeholder="1~60位字符">
                    </div>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-md-2 col-sm-3 col-xs-4 control-label"> 箱规 </label>
                <div class="col-md-6 col-sm-8 col-xs-8">
                    <div class="iconic-input right">
                        <input id="boxSpec" name="boxSpec" type="text" class="form-control spinner"  maxlength="60" placeholder="1~60位字符">
                    </div>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-md-2 col-sm-3 col-xs-4 control-label"> 条码 </label>
                <div class="col-md-6 col-sm-8 col-xs-8">
                    <div class="iconic-input right">
                        <input id="goodsBarCode" name="goodsBarCode" type="text" class="form-control spinner"  maxlength="20" placeholder="1~20位字符">
                    </div>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-md-2 col-sm-3 col-xs-4 control-label"> 商品类型 </label>
                <div class="col-md-6 col-sm-8 col-xs-8">
                    <div class="iconic-input right">
                        <select name="goodsType" class="form-control" data-url="goods/listGoodsType" data-value-field="id" required data-text-field="typeName"></select>
                    </div>
                </div>
            </div>
        </div>
    </div>
</form>

</body>
</html>
<script type="text/javascript">

    //下面用于图片上传预览功能
    function setImagePreview() {
        var docObj = document.getElementById("doc");
        var file = docObj.files[0];
        var fileSize =file.size;
//        console.log(fileSize);
        if(fileSize>512000){
            $.error("图片超过要求上传大小，请重新上传");
            return false;
        }
        var formData = new FormData();
        formData.append("files", file);
        $.ajax({
            url: "file/upload",
            type: "POST",
            data: formData,
            processData : false,// 告诉jQuery不要去处理发送的数据
            contentType : false,// 告诉jQuery不要去设置Content-Type请求头
            beforeSend:function(){
                console.log("正在进行，请稍候");
            },
            success : function(data) {
                if (data.code !== '0') {
                    $.error(data.msg);
                } else {
                    var url = data.data[0].url;
                    $("#goodsImg").val(url);
                    $("#preview").attr("src", url);
                }
            },
            error : function() {
                $.error("上传失败");
            }
        });
    }

</script>

